package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberService;

import com.alibaba.dubbo.config.annotation.Reference;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: ZHT
 * @Dtae: 2021/11/15 15:31
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping(path = "/member")
@CrossOrigin
public class MemberController {


    @Reference(version = "1.0.0")
    private MemberService memberService ;

    /*
    * 分页查询
    *
    * */
    @RequestMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        log.info("接收的参数为{}",queryPageBean);
        try {
            PageResult pageResult = this.memberService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }}
    /*
    * 弹出编辑
    * */
    @GetMapping("{userId}")
    public Result tanChu(@PathVariable("userId") Integer userId){
        log.info("接收的参数为{}",userId);

        try {
            Member member = this.memberService.tanChu(userId);
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS, member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }}

    /*
    * 修改数据
    * */
    @PutMapping("/update")
    public Result update(@RequestBody Member member ){

        try {
            Boolean update = memberService.update(member);
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }}
    /*
     * 新增数据
     * */
    @PostMapping("/add")
    public Result add(@RequestBody Member member ){

        try {
            Boolean add = memberService.add(member);
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }}

    @PutMapping("/{userId}/{status}")
    public Result zhuanTai (@PathVariable("userId") Integer userId , @PathVariable("status") Integer status){

        try {
            memberService.updateStatus(userId, status);

            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }}
    }




